//
//  ZWWelcomeViewController.swift
//  张伟的微博
//
//  Created by 伟 on 16/3/2.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class ZWWelcomeViewController: UIViewController {

    
    private var iconViewBottom :NSLayoutConstraint?
    
    override func loadView() {
        super.loadView()
        prepraUI()
        //显示沙盒保存头像
//        if let avatar_large = ZWUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
//        iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
//        }
    
        //获得最新头像
        ZWUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
            
            print("获取信息出错")
                return
            
                
            }
           self.setIcon()
            
        }

    }
    func setIcon(){
        let avatar_large = ZWUserAccountViewModel.sharedInstance.userAccount?.avatar_large
        
        iconView.sd_setImageWithURL(NSURL(string: avatar_large!), placeholderImage: UIImage(named: "avatar_default_big"))
    
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
        
    }
    private func moveAnimation() {
//        iconViewBottom?.constant = -UIScreen.mainScreen().bounds.size.height - iconViewBottom!.constant
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset( -(UIScreen.mainScreen().bounds.size.height - 160))
        }
        UIView.animateWithDuration(1.3, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.nameLabel.alpha = 1
            })
        }) { (_) -> Void in
            
            //切换到MAIN
           AppDelegate.outSwitchViewController(true)
         
        }
    }
    
    
    
    
    //添加背景图片，并设置约束
    private func prepraUI() {
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
//        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        //背景
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView": backgroundImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : backgroundImageView]))
        //背景snp框架
        backgroundImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }

        //头像
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 85))
//         view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 85))
//         view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//         iconViewBottom = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//            view.addConstraint(iconViewBottom!)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.width.height.equalTo(85)
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }
        
        //label
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    
    

   //MARK : - 懒加载背景和头像and文字
    private lazy var backgroundImageView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    
    private lazy var iconView :UIImageView = {
        let imgageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imgageView.layer.cornerRadius = 42.5
        imgageView.layer.masksToBounds = true
        return imgageView
    }()
    
    private lazy var nameLabel :UILabel = {
        let nameLabel = UILabel()
        nameLabel.text = "欢迎归来"
        nameLabel.sizeToFit()
        nameLabel.alpha = 0
        return nameLabel
    }()

}
